# -*- coding: utf-8 -*-
"""
Created on Thu Sep 30 15:20:01 2021

@author: Administrator
"""

class ListNode(object):  #链表数据类型
    def __init__(self,val,next = None):
        self.val = val
        self.next = next;
class Solution:
    def deleteDuplicates(self,head):
        if None == head or None == head.next: #存在只有头节点或者连头节点都没有的链表返回本身
            return head
        
        new_head = ListNode(-1)   #自定义一个值为-1的头节点
        new_head.next = head
        
        parent = new_head  #筛出重复数值的关键！！
        cur = head
        while None != cur and None != cur.next: #重点理解！！！！！！
            if cur.val == cur.next.val:
                val = cur.val
                while None != cur and val == cur.val:
                    cur = cur.next
                parent.next = cur
            else:
                cur = cur.next
                parent = parent.next
        return new_head.next
    
if __name__ == "__main__":
    node1 = ListNode(1) #制造链表1->1->1->2->3
    node2 = ListNode(1)
    node3 = ListNode(1)
    node4 = ListNode(2)
    node5 = ListNode(3)
    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    
    list1 = []  #存储处理后的链表数值
    solution = Solution()
    print("初始链表：",[node1.val,node2.val,node3.val,node4.val,node5.val])
    
    newlist = solution.deleteDuplicates(node1) #核心：处理重复数值
    while(newlist):
        list1.append(newlist.val)
        newlist = newlist.next
    print("删除重复元素后的结果：",list1)